package com.eagle.roomcrawler.config;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by gx
 * Date: 2017-09-13
 * Time: 21:41
 */
@Configuration
public class ElasticsearchConfig {

    @Value("${spring.data.elasticsearch.cluster-domain}")
    private String clusterDomain ;

    @Value("${spring.data.elasticsearch.cluster-name}")
    private String clusterName;

    @Bean
    public TransportClient client() throws UnknownHostException {
        //设置端口名字
        InetSocketTransportAddress node = new InetSocketTransportAddress(
                InetAddress.getByName(clusterDomain),
                9300
        );
        //设置名字
        Settings settings = Settings.builder().put("cluster.name",clusterName).build();

        TransportClient client =  new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);
        return client;
    }

}
